<template>
  <div id="login">
    <el-card class="box-card">
      <div style="display: flex;align-items: center;">
        <span style="font-weight: bold;font-size: 25px;margin-left: 30%;">权限管理系统</span>
      </div>
      
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="ruleForm">
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="ruleForm.username" clearable  size="small" style="width: 200px;"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input type="password" v-model="ruleForm.password" clearable  size="small" style="width: 200px;"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 10px;">
          <el-button type="primary" @click="submitForm('ruleForm')" >登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import userApi from '@/api/user'
export default {
  data(){
    return{
      ruleForm:{
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
      submitForm(formName) {
        this.loading = true
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$store.dispatch('user/login', this.ruleForm).then(() => {
              this.$router.push({ path: this.redirect || '/' })
            }).catch(() => {
            })
            console.log(1);
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
  },
}
</script>

<style>
.ruleForm{
  margin-top: 20px;
}
#login {
  background: url("../../assets/images/login.jpg") no-repeat;
  background-position: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;

}
.box-card{
  opacity: 0.9;
  width: 350px;
  height: 320px;
  margin-top: 180px;
  margin-left: 580px;
  border-radius: 8%;
}
</style>